package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.ResultVo;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.service.EduChapterService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2024-12-09
 */

@RestController
@CrossOrigin
@RequestMapping("/eduservice/chapter")
public class EduChapterController {


    @Autowired
    private EduChapterService eduChapterService;
    //根据课程id查询课程基本信息

    @GetMapping("getChapterVideo/{courseId}")
    public ResultVo getChapterVideo(@PathVariable String courseId){
        List<ChapterVo> list = eduChapterService.getChapterVideoByCourseId(courseId);
        return ResultVo.ok().data("allChapterVideo",list);
    }
    @ApiOperation(value = "新增章节")
    @ApiParam(name = "eduChapter", value = "章节对象", required = true)
    @PostMapping("addChapter")
    public ResultVo addChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.save(eduChapter);
        return ResultVo.ok();
    }

    //根据章节id查询
    @ApiOperation(value = "根据ID查询章节")
    @ApiParam(name = "chapterId", value = "章节ID", required = true)
    @GetMapping("getChapterInfo/{chapterId}")
    public ResultVo getChapterInfo(@PathVariable String chapterId){
        EduChapter eduChapter = eduChapterService.getById(chapterId);
        return ResultVo.ok().data("chapter",eduChapter);
    }


    //修改章节
    @ApiOperation(value = "修改章节")
    @PostMapping("updateChapter")
    public ResultVo updateChapter(@RequestBody EduChapter eduChapter) {
        eduChapterService.updateById(eduChapter);
        return ResultVo.ok();
    }

    //删除的方法
    @ApiOperation(value = "根据章节id删除")
    @DeleteMapping("{chapterId}")
    public ResultVo deleteChapter(@PathVariable String chapterId) {
        boolean flag = eduChapterService.deleteChapter(chapterId);
        if(flag) {
            return ResultVo.ok();
        } else {
            return ResultVo.error();
        }

    }



}

